import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../network/Api.dart';
import '../../theme/theme_color.dart';
import '../../utils/snack_bar.dart';
import '../../widget/icon_text.dart';
import '../user/profile.dart';


class MatcherListUi extends StatefulWidget {
  const MatcherListUi({Key? key}) : super(key: key);

  @override
  State<MatcherListUi> createState() => _MatcherListWidgetState();
}

class _MatcherListWidgetState extends State<MatcherListUi> {
  late Size _size;
  late double _screenWidth, _screenHeight;
  late ScrollController _scrollController;

  /// 列表
  final List _matchmakerList = [];
  int _pageNum = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController = ScrollController();
    _scrollController.addListener(() {
      _pageNum++;
      _getList();
    });
    _getList();
  }

  @override
  Widget build(BuildContext context) {
    _size = MediaQuery.of(context).size;
    _screenWidth = _size.width;
    _screenHeight = _size.height;
    return AppWidget.navTitleBar(
      elevation:0.2,
      bgColor: ThemeColor.grayF8F8F8Color,
      title: "红娘",
      childWidget: RefreshIndicator(
          onRefresh: _onRefresh,
          child: GridView.custom(
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 1,
              mainAxisSpacing: 14,
              crossAxisSpacing: 0,
              childAspectRatio: 347 / 101,),
            childrenDelegate: SliverChildBuilderDelegate((context, index) {
              return _buildItemView(index);
            }, childCount: _matchmakerList.length),
          )),
    );
  }

  /// 列表item
  Widget _buildItemView(int index) {
    final data = _matchmakerList[index];
    double itemWidth = 62;
    bool isWoman= data['realSex'] == 2?true:false;
    Color textColor=isWoman?ThemeColor.womanTextColor:ThemeColor.manTextColor;
    const Color black4C4E50Color =  Color(0xFF4C4E50);
    final introduce=  data['introduce'];

    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (builder) => UserProfileUi(
                  memberId: data["memberId"],
                )));
      },
      child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 14),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10),
          ),
          child: Row(
            children: [
              Stack(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(itemWidth / 2.0),
                    child: Image.network(data['avatarImg'],
                        width: itemWidth,
                        height: itemWidth,
                        fit: BoxFit.cover),
                  ),
                  Positioned(
                    right: 5,
                    bottom: 5,
                    child: data['singleState'] == 1
                        ? Image.asset(
                      "images/ic_fdsbzh.png",
                      width: 33,
                      height: 23,
                      fit: BoxFit.fill,
                    )
                        : Container(),
                  )
                ],
              ),
              const SizedBox(
                width: 12,
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  // 名称、等级
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ConstrainedBox(
                        constraints: BoxConstraints(maxWidth: 1.sw - 150),
                        child: Text(
                          data['nickName'],
                          style: const TextStyle(
                            color: Colors.black,
                            fontSize: 14,
                          ),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  // 地址性别
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        alignment: Alignment.center,
                        decoration: ShapeDecoration(
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(7),
                            ),
                            color: isWoman
                                ? ThemeColor.womanColor
                                : ThemeColor.manColor),
                        width: 40,
                        height: 14,
                        child: IconText(
                          data['age'].toString() == 'null'
                              ? '0'
                              : data['age'].toString(),
                          icon: Icon(
                              isWoman
                                  ? Icons.female
                                  : Icons.male,
                              color: textColor),
                          iconSize: 14,
                          style:  TextStyle(
                            color: textColor,
                            fontSize: 12,
                          ),
                        ),
                      ),
                      const SizedBox(
                        width: 5,
                      ),
                      Container(
                        decoration: BoxDecoration(color: const Color(0xFFF5F5F7),borderRadius: BorderRadius.circular(12)),
                        padding: const EdgeInsets.symmetric(vertical:2,horizontal: 6),
                        alignment: Alignment.center,
                        child: Text(
                          "${data['homeAddr'] ?? ""}",
                          style:const TextStyle(
                              color: black4C4E50Color,
                              fontSize: 10,
                              overflow: TextOverflow.ellipsis),
                        ),
                      )
                    ],
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Text(
                    introduce??'他很懒，什么都没写' ,
                    style: TextStyle(
                      fontSize: 10,
                      color: introduce==null?ThemeColor.lineColor: black4C4E50Color,
                    ),
                  ),
                ],
              ),
              Expanded(child: Container()),
              Container(
                width: 48,
                height: 25,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(25 / 2.0),
                    border:
                    Border.all(color: ThemeColor.appColor, width: 1.0)),
                alignment: Alignment.center,
                child: Text(
                  '看看',
                  style: TextStyle(fontSize: 12, color: ThemeColor.appColor),
                ),
              ),
            ],
          )),
    );
  }

  Future<void> _onRefresh() async {
    _pageNum = 1;
    await _getList();
  }

  _getList() async {
    final res = await Api.getInstance().getMatchmakerList(1000, _pageNum);
    if (res['code'] == 0) {
      final data = res['data'];
      if (data != null) {
        List list = data['list'];
        if (list.isNotEmpty == true) {
          if (_pageNum <= 1) {
            _matchmakerList.clear();
          }
          setState(() {
            _matchmakerList.addAll(list);
          });
        }
      }
    } else {
      ThemeSnackBar.show(context, res['msg']);
    }
  }
}
